﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using DAL;
using DataContext;
using Utils;
using Config;
using Entity;
using Library;

namespace BLL
{
    public class CtrRoleFunction
    {
        /// <summary>
        /// Thêm mới một RoleFunction
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="functionID"> </param>
        /// <returns></returns>
        public int InsertRoleFunction(int roleID, int functionID)
        {
            var ctrFunction = new CtrFunction();
            var functionInfo = ctrFunction.GetInfoFunction(functionID);
            if (functionInfo.ParentId == -1)
            {
                var functionIDs = "";
                foreach (var item in GetFunctionChildNotInRole(functionID, roleID))
                {
                    functionIDs += item.Id.ToString(CultureInfo.InvariantCulture) + ',';
                }
                functionIDs = functionIDs.Substring(0, functionIDs.Length - 1);
                var ret="";
                return DB.AdminInstance.uspRoleFunctionInsertMulti(roleID, functionIDs, ref ret);
            }
            return DB.AdminInstance.uspRoleFunctionInsert(roleID, functionID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="functionsIDs"> </param>
        /// <returns></returns>
        public string InsertRoleFunctionMulti(int roleID, string functionsIDs)
        {
            var ret = "";
            var spFunctionIDs = functionsIDs.Split(',');
            foreach (var functionID in spFunctionIDs)
            {
                var result = InsertRoleFunction(roleID, Convert.ToInt32(functionID));
                ret += result.ToString(CultureInfo.InvariantCulture);
            }
            return ret;
        }

        /// <summary>
        /// Xá một RoleFunction
        /// </summary>
        /// <returns></returns>
        public int DeleteRoleFunction(int roleID, int menuID)
        {
            return DB.AdminInstance.uspRoleFunctionDelete(roleID, menuID);
        }

        /// <summary>
        /// Xóa đồng thời nhiều RoleFunction
        /// </summary>
        /// <returns></returns>
        public int DeleteRoleFunctionMulti(int roleID, string menuIDs)
        {
            return DB.AdminInstance.uspRoleFunctionDeleteMulti(roleID, menuIDs);
        }

        /// <summary>
        /// Lấy danh sách function con của một parrentFunction mà thuộc Role truyền vào
        /// </summary>
        /// <param name="parrentID"></param>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<uspFunctionGetChildInRoleResult> GetFunctionChildInRole(int parrentID, int roleID)
        {
            return DB.AdminInstance.uspFunctionGetChildInRole(AdminUtils.ServicesID, parrentID, roleID).ToList();
        }

        /// <summary>
        /// Lấy danh sách function con của một parrentFunction mà không thuộc Role truyền vào
        /// </summary>
        /// <param name="parrentID"></param>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<uspFunctionGetChildNotInRoleResult> GetFunctionChildNotInRole(int parrentID, int roleID)
        {
            return DB.AdminInstance.uspFunctionGetChildNotInRole(AdminUtils.ServicesID, parrentID, roleID).ToList();
        }

        /// <summary>
        /// Lấy danh sách Function không thuộc Role truyền vào
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public ClassExtend<int, ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildNotInRoleResult>> GetFunctionNotInRole(int roleID)
        {
            var objReturn = new ClassExtend<int, ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildNotInRoleResult>>();
            var ctrFunction = new CtrFunction();
            var listFunction = ctrFunction.GetChild(-1, -1).Items;
            foreach (var item in listFunction)
            {
                var listFunctionChild = new ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildNotInRoleResult>
                                            {Info = item};
                var lstChild = GetFunctionChildNotInRole(item.Id, roleID);
                listFunctionChild.Items = lstChild;
                listFunctionChild.TotalRecord = lstChild.Count;
                objReturn.Items.Add(listFunctionChild);
            }
            objReturn.TotalRecord = objReturn.Items.Count;
            return objReturn;
        }

        /// <summary>
        /// Lấy danh sách Function thuộc Role truyền vào
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public ClassExtend<int, ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildInRoleResult>> GetFunctionInRole(int roleID)
        {
            var objReturn = new ClassExtend<int, ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildInRoleResult>>();
            var ctrFunction = new CtrFunction();
            var listFunction = ctrFunction.GetChild(-1, -1).Items;
            foreach (var item in listFunction)
            {
                var listFunctionChild = new ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildInRoleResult>
                                            {Info = item};
                var lstChild = GetFunctionChildInRole(item.Id, roleID);
                listFunctionChild.Items = lstChild;
                listFunctionChild.TotalRecord = lstChild.Count;
                objReturn.Items.Add(listFunctionChild);
            }
            objReturn.TotalRecord = objReturn.Items.Count;
            return objReturn;
        }

        /// <summary>
        /// Lấy danh sách Role theo FunctionID
        /// </summary>
        /// <returns></returns>
        public ClassExtend<int, uspRoleFunctionGetRoleInFunctionResult> GetListRoleInFunction(int functionID)
        {
            var objReturn = new ClassExtend<int, uspRoleFunctionGetRoleInFunctionResult>
                                {Items = DB.AdminInstance.uspRoleFunctionGetRoleInFunction(functionID).ToList()};
            objReturn.TotalRecord = objReturn.Items.Count;
            objReturn.Info = 0;
            return objReturn;
        }

        /// <summary>
        /// Lấy danh sách Role mà Funtion k có quyền
        /// </summary>
        /// <returns></returns>
        public ClassExtend<int, uspRoleFunctionGetRoleNotInFunctionResult> GetListRoleNotInFunction(int FunctionID)
        {
            var objReturn = new ClassExtend<int, uspRoleFunctionGetRoleNotInFunctionResult>
                                {Items = DB.AdminInstance.uspRoleFunctionGetRoleNotInFunction(FunctionID).ToList()};
            objReturn.TotalRecord = objReturn.Items.Count;
            objReturn.Info = 0;
            return objReturn;
        }

    }
}
